<?
include $r."_config/site.php";

/*
$file_list = array(..., $file_item, ...)
$file_item = array(
	"file" => "c:\1.php", 
	"name" => "1.php" // File name to display
)
*/

function adv_mail($to, $subject, $message, $headers, $file_list=array())
{
	//$message = stripcslashes($message)
	$message = str_replace("\r", "", $message);

	$boundary = "----------587433454358";
	$nl = "\n";
	if (count($file_list) > 0)
	{
		// Multi-part message
		$headers .=
			"MIME-Version: 1.0".$nl.
			"Content-Type: multipart/mixed; boundary=\"".$boundary."\"".$nl;
			//"This is a multi-part Content in MIME format.".$nl;
	
		$ext_message = 
			"--".$boundary.$nl.
			"Content-Type: text/plain; charset=us-ascii".$nl.
			"Content-Transfer-Encoding: 7bit".$nl.
			$nl.
			$message.$nl.
			"--".$boundary;

		for ($i=0; $i<count($file_list); $i++)
		{
			$v = $file_list[$i];
			$fd = fopen ($v["file"], "rb");
			$contents = fread ($fd, 100000000/*filesize($v["file"])*/);
			fclose ($fd);
			
		$ext_message .= 
			$nl.
			"Content-Type: text/plain; name=\"".$v["name"]."\"".$nl.
			"Content-transfer-encoding: base64".$nl.
			"Content-Disposition: attachment; filename=\"".$v["name"]."\"".$nl.
			$nl.
			chunk_split(base64_encode($contents), 76, $nl).$nl.
			"--".$boundary;
		}
		
		$ext_message .= "--";
	}
	else
	{
		// Simple text message
		$ext_message = $message;
	}

	// Append mailer id to header
	global $g_site_root;
	if ($headers != "") $headers .= "\n";
	$headers .= "X-Mailer: ".$g_site_root." with PHP/".phpversion();

	if (0) 
	{
		echo "<pre>\n";
		echo "TO: ".$to."\n";
		echo "SUBJ: ".$subject."\n\n";
		echo $headers."\n\n";
		echo $ext_message;
		echo "</pre>\n";
		die("");
	}

	@mail($to, $subject, $ext_message, $headers);
}


function report_error($src, $error_message)
{
	global $_SERVER, $g_site_root;
	global $ses_account_id, $ses_login_password,$ses_is_logged;
    
    $nl = "\n";
    $separator = "------------------------------------------".$nl;

    $mail_subj = 
		"ERROR with ".$src." at http://".$g_site_root."/";

    $mail_body =
        $separator.
        "Timestamp:    ".date(DATE_RFC822).$nl.
        "Error source: ".$src.$nl.
        "Site:         ".$g_site_root.$nl.
    	"User agent:   ".@$_SERVER["HTTP_USER_AGENT"]."\n".
		"Remote addr:  ".@$_SERVER["REMOTE_ADDR"]."\n".
        $separator.
        
        "COOKIES:".$nl.
		"ses_account_id:     ".var_export($ses_account_id, true)."\n".
		"ses_login_password: ".var_export($ses_login_password, true)."\n".
		"ses_is_logged:      ".var_export($ses_is_logged, true)."\n".
        $separator.
        
        "ERROR:".$nl.
		$error_message.$nl.
        $separator.

    	"DEBUG BACKTRACE:"."\n".
        var_export(debug_backtrace(), true).$nl.
        $separator;

	@mail(
		"all@w3monitor.com",
        $mail_subj,
		$mail_body,
		"FROM: none@w3monitor.com");
}
?>